package com.learn.util.prop;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

/**
 * 功能：Prop 能从 classpath 或文件对象中，读取配置
 *
 * @author wsh
 * @date 2021/1/25
 */
public class Prop {

    private static final Logger logger = LoggerFactory.getLogger(Prop.class);

    private Properties properties = null;

    /**
     * 功能：加载配置文件
     *
     * @param fileName 配置文件
     * @return
     * @author wsh
     * @date 2021/1/25
     * @see #Prop(String, String)
     */
    public Prop(String fileName) {
        this(fileName, PropKit.DEFAULT_ENCODING);
    }

    /**
     * 功能：加载配置文件
     * <p>
     * 例如：<br>
     * Prop prop = new Prop("my_config.txt", "UTF-8");<br>
     * String userName = prop.get("userName");<br>
     * prop = new Prop("com/final/file_in_sub_path_of_classpath.txt", "UTF-8");<br>
     * String value = prop.get("key");
     * </p>
     *
     * @param fileName 配置文件
     * @param encoding 配置文件读取编码
     * @return
     * @author wsh
     * @date 2021/1/25
     */
    public Prop(String fileName, String encoding) {
        InputStream inputStream = null;
        try {
            inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            if (inputStream == null) {
                throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
            }
            properties = new Properties();
            properties.load(new InputStreamReader(inputStream, encoding));
        } catch (IOException e) {
            throw new RuntimeException("Error loading properties file.", e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
    }

    /**
     * 功能：加载配置文件
     *
     * @param file 配置文件对象
     * @return
     * @author wsh
     * @date 2021/1/25
     * @see #Prop(File, String)
     */
    public Prop(File file) {
        this(file, PropKit.DEFAULT_ENCODING);
    }

    /**
     * 功能：加载配置文件
     * <p>
     * 例如：<br>
     * Prop prop = new Prop(new File("/var/config/my_config.txt"), "UTF-8");<br>
     * String userName = prop.get("userName");
     * </p>
     *
     * @param file     配置文件对象
     * @param encoding 配置文件读取编码
     */
    public Prop(File file, String encoding) {
        if (file == null) {
            throw new IllegalArgumentException("File can not be null.");
        }
        if (file.isFile() == false) {
            throw new IllegalArgumentException("Not a file : " + file.getName());
        }

        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            properties = new Properties();
            properties.load(new InputStreamReader(inputStream, encoding));
        } catch (IOException e) {
            throw new RuntimeException("Error loading properties file.", e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
    }

    public String get(String key) {
        return properties.getProperty(key);
    }

    public String get(String key, String defaultValue) {
        String value = get(key);
        return (value != null) ? value : defaultValue;
    }

    public Integer getInt(String key) {
        String value = get(key);
        return (value != null) ? Integer.parseInt(value) : null;
    }

    public Integer getInt(String key, Integer defaultValue) {
        String value = get(key);
        return (value != null) ? Integer.parseInt(value) : defaultValue;
    }

    public Long getLong(String key) {
        String value = get(key);
        return (value != null) ? Long.parseLong(value) : null;
    }

    public Long getLong(String key, Long defaultValue) {
        String value = get(key);
        return (value != null) ? Long.parseLong(value) : defaultValue;
    }

    public Boolean getBoolean(String key) {
        String value = get(key);
        return (value != null) ? Boolean.parseBoolean(value) : null;
    }

    public Boolean getBoolean(String key, Boolean defaultValue) {
        String value = get(key);
        return (value != null) ? Boolean.parseBoolean(value) : defaultValue;
    }

    public boolean containsKey(String key) {
        return properties.containsKey(key);
    }

    public Properties getProperties() {
        return properties;
    }
}
